<?php

/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author oriotiori

 */

class Admin_Model_Taxes extends Zend_Db_Table_Abstract{

    protected $_name = 'taxes';

/**
 * get Categories
 * @param int $id
 * @return array $data
 */
    public function getTaxes()
    {
        $TO = new Admin_Model_TO_Taxes();
        
        $data=array();
        try{
            $sql=$this->getAdapter()
            		->select()
            		->from(
                        array('t' => TABLE_PREFIX.$this->_name),
                        array('t.*')
                    )                    
            		->order('t.Id DESC');


            $res = $this->getAdapter()->fetchAll($sql);
            $rowCount=count($res);
			
            if($rowCount>0){
                foreach($res as $dt){                	
                    $TO->setId($dt['Id']);
                    $TO->setName($dt['Name']);
                    $TO->setPercent($dt['Percent']);
                    $TO->add();
				}
                $data = $TO->getAllData();
                return $data;
            }
        }catch(Exception $e)
        {
            $error=array($e->getFile(), 'Exception: '.$e->getMessage());
            $ExceptionHandler= new Default_Model_ExceptionHandler();
            $ExceptionHandler->saveErrors($error);
        }
        return false;
    }
    
    
  
/**
 * @param int $id
 * @return array $data
 */
    public function getTaxesById($id=0)
    {
        $TO = new Admin_Model_TO_Taxes();
        $data=array();
        try{
            $sql=$this->getAdapter()
            		->select()
            		->from(
                        array('t' => TABLE_PREFIX.$this->_name),
                        array('t.*')
                    )
            		->where("t.Id=?", $id);
            
            $res = $this->getAdapter()->fetchAll($sql);
            $rowCount=count($res);
			
            if($rowCount>0){
                foreach($res as $dt){                	
                    $TO->setId($dt['Id']);
                    $TO->setName($dt['Name']);
                    $TO->setPercent($dt['Percent']);
				}
                $data = $TO->getAllVars();
                return $data;
            }
        }catch(Exception $e)
        {
            $error=array($e->getFile(), 'Exception: '.$e->getMessage());
            $ExceptionHandler= new Default_Model_ExceptionHandler();
            $ExceptionHandler->saveErrors($error);
        }
        return false;
    }
    

  
    /**
     * @param Array $to
     * @return int $lastId
     */
    public function addTaxes($to)
    {
        try{
            $data = array(
                  	'Percent'   => $to['Percent'],
                  	'Name'  	=> (!empty($to['Name'])) ? $to['Name'] : new Zend_Db_Expr('null')
            );
			
            $res = $this->getAdapter()->insert(TABLE_PREFIX.$this->_name ,$data, $where);
            $lastId= $this->getAdapter()->lastInsertId();
            return $lastId;
        }catch(Exception $e)
        {
            $error=array($e->getFile(), 'Exception: '.$e->getMessage());
            $ExceptionHandler= new Default_Model_ExceptionHandler();
            $ExceptionHandler->saveErrors($error);
        }
        return false;
    }
    		
  
    /**
     * @param Array $to
     * @return int $lastId
     */
    public function updateTaxesById($to)
    {
    	
        try{
            $data = array(
                  	'Percent'	=> $to['Percent'],
                  	'Name'  	=> (!empty($to['Name'])) ? $to['Name'] : new Zend_Db_Expr('null')
            );
			$where[]="Id=". $to['Id'];

            $res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);
            return true;

        }catch(Exception $e)
        {
            $error=array($e->getFile(), 'Exception: '.$e->getMessage());
            $ExceptionHandler= new Default_Model_ExceptionHandler();
            $ExceptionHandler->saveErrors($error);
        }
        return false;
    }
    		
    
    
    
   /**
    * @param int id
    * @return boolean
    */
   public function deleteTaxesById($id=0)
   {
   	try{
   		$res=$this->getAdapter()->delete(TABLE_PREFIX.$this->_name ,"Id=".$id);
   		return true;
   	}catch(Exception $e)
   	{
   		$error=array($e->getFile(), 'Exception: '.$e->getMessage());
   		$ExceptionHandler= new Default_Model_ExceptionHandler();
   		$ExceptionHandler->saveErrors($error);
   	}
   	return false;
   }
    
    
    
    
    
}
